package com.janson.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.janson.entity.Job;
import com.janson.entity.JobCategory;
import com.janson.service.impl.JobCategoryServiceImpl;
import com.janson.service.impl.JobServiceImpl;
import com.janson.utils.MyUtils;
import jdk.nashorn.internal.scripts.JO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import sun.security.provider.MD2;

import javax.jws.WebParam;
import javax.print.attribute.standard.JobMediaSheetsCompleted;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author janson
 * @since 2022-03-30
 */
@Controller
public class JobController {
    private boolean flag = false;
    @Autowired
    JobServiceImpl jobService;
    @Autowired
    JobCategoryServiceImpl jobCategoryService;
    @GetMapping("/job")
    public String jobList(Model model){
        Page<Job> pageParam = new Page<>(1,10);
        jobService.page(pageParam, new QueryWrapper<Job>().orderByDesc("gmt_create"));
        List<Job> jobList = pageParam.getRecords();
        model.addAttribute("blogList",jobList);
        model.addAttribute("pageParam",pageParam);

        List<JobCategory> jobCategories = jobCategoryService.list(null);
        model.addAttribute("categoryList",jobCategories);

        if (flag){
            model.addAttribute("issueSuccess","兼职发布成功");
        }
        return "blog/list";
    }

    //分页查询
    @GetMapping("/job/{page}/{limit}")
    public String jobListPage(@PathVariable int page,@PathVariable int limit,Model model){
        //保证不会在第一页之前在查找
        if(page < 1){
            page = 1;
        }
        Page<Job> pageParam = new Page<>(page, limit);
        IPage<Job> page1 = jobService.page(pageParam,new QueryWrapper<Job>().orderByDesc("gmt_create"));
        //结果
        List<Job> jobRecords = pageParam.getRecords();
        model.addAttribute("blogList",jobRecords);
        model.addAttribute("pageParam",pageParam);
        //分类信息
        List<JobCategory> jobCategories = jobCategoryService.list(null);
        model.addAttribute("categoryList",jobCategories);
        return "blog/list";
    }
    //f发布兼职
    @GetMapping("/job/write")
    public String tuIssue(Model model){
        List<JobCategory> jobCategories = jobCategoryService.list(null);
        model.addAttribute("categoryList",jobCategories);
        return "blog/write";
    }
    @PostMapping("/job/write")
    public String issueJob(Job job1){

        Job job = new Job();
        job.setJid(MyUtils.getUID());
        job.setAuthorAvatar(job1.getAuthorAvatar());
        job.setAuthorId(job1.getAuthorId());
        job.setAuthorName(job1.getAuthorName());
        job.setContent(job1.getContent());
        job.setTitle(job1.getTitle());
        job.setGmtCreate(MyUtils.getTime());
        job.setGmtUpdate(MyUtils.getTime());
        JobCategory category = jobCategoryService.getById(job1.getCategoryId());
        job.setCategoryId(category.getId());
        job.setCategoryName(category.getCategory());
        job.setSort(0);
        job.setViews(0);

        boolean save = jobService.save(job);
        if (save){
            flag = true;
        }
        return "redirect:/job";
    }

    @GetMapping("/job/read/{jid}")
    public String read(@PathVariable("jid") String jid, Model model){
        Job job = jobService.getOne(new QueryWrapper<Job>().eq("jid",jid));
        //统计阅读次数
        job.setViews(job.getViews()+1);
        jobService.updateById(job);
        model.addAttribute("blog",job);
        return "blog/read";
    }

    //删除操作
    @GetMapping("/job/delete/{uid}/{Jid}")
     public String deleteJob(@PathVariable String uid,@PathVariable String Jid,Model model){
        Job job = jobService.getOne(new QueryWrapper<Job>().eq("jid", Jid));
        if(!job.getAuthorId().equals(uid)){
            MyUtils.print("禁止非法删除");
        }
        boolean remove = jobService.removeById(job);
        return "redirect:/job";
    }
    @GetMapping("/job/editor/{Uid}/{Jid}")
    public String editJob(@PathVariable String Uid,@PathVariable String Jid,Model model){
        Job job = jobService.getOne(new QueryWrapper<Job>().eq("jid", Jid));
        if (!job.getAuthorId().equals(Uid)){
            MyUtils.print("禁止非法编辑");
            return "redirect:/job";
        }
        model.addAttribute("blog",job);
        List<JobCategory> categoryList = jobCategoryService.list(null);
        model.addAttribute("categoryList",categoryList);
        return "blog/editor";
    }
    @PostMapping("/job/editor")
    public String editorJob(Job job){
        Job queryJob = jobService.getOne(new QueryWrapper<Job>().eq("jid", job.getJid()));
        queryJob.setTitle(job.getTitle());
        queryJob.setCategoryId(job.getCategoryId());
        JobCategory category = jobCategoryService.getById(job.getCategoryId());
        queryJob.setCategoryName(category.getCategory());
        queryJob.setContent(job.getContent());
        queryJob.setGmtUpdate(MyUtils.getTime());

        jobService.updateById(queryJob);
        return "redirect:/job/read/" + queryJob.getJid();
    }

    //查询自己发布的兼职
    @GetMapping("/myIssue/{uid}")
    public String myIssue(@PathVariable String uid,Model model){

        Page<Job> jobPage = new Page<>(1,10);
        IPage<Job> iPage = jobService.page(jobPage, new QueryWrapper<Job>().eq("author_id", uid));

        List<Job> myJobList = jobPage.getRecords();
        model.addAttribute("blogList",myJobList);
        model.addAttribute("pageParam",jobPage);

        List<JobCategory> jobCategories = jobCategoryService.list(null);
        model.addAttribute("categoryList",jobCategories);
        return "blog/list";
    }

}

